package com.nj.dms.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.PaperMoldDocument;
import com.nj.dms.dao.PaperMoldDocumentDao;
import com.nj.dms.dao.impl.PaperMoldDocumentDaoImpl;

/**
 * 保存修改的纸版文档
 */
@WebServlet("/SavePaperMoldDocument")
public class SavePaperMoldDocument extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 接收修改后的数据
        int id = new Integer(request.getParameter("id"));
        String title = request.getParameter("title");
        String rollNum = request.getParameter("rollNum");
        String location = request.getParameter("location");
        int thick = new Integer(request.getParameter("thick"));
        String description = request.getParameter("description");

        // 获取用户名
        String userName = (String) request.getSession().getAttribute("userName");

        // 设置审核状态

        int audit = thick == 1 ? 1 : 0;

        PaperMoldDocument pmd = new PaperMoldDocument(id, title, description, thick, audit, rollNum, new Date(), location);

        PaperMoldDocumentDao pmddi = new PaperMoldDocumentDaoImpl();

        if (pmddi.savePaperMoldDocument(pmd)) {

            // 修改成功，跳转到纸版文档列表

            response.sendRedirect("MyPaperMoldDocumentList");

        } else {
            // 修改失败，跳转到修改表单
            response.sendRedirect("UpdatePaperMoldDocument?id=" + id);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
